unit oFormFL;

{
	created   26.3.2010
	author   	Miroslav Mintal
	purpose
}


interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls,
  Mathematic, Vector, RenderContext, View, Pedestrian, Obstacle, Path, uOSPRand, GL, GLu,
  Menus, Buttons, CheckLst, oProcessObjectConstants, oEntityTypeConstants,
   oProcessObjects, OProcessItem, oEditorManager, schodyFrm,
  oFrameFL, oFrameScene, ComCtrls;

type
  TformFL = class(TForm)
    TfEditor1: TfEditor;
    FrameFL1: TFrameFL;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure fFL1btnSaveClick(Sender: TObject);
    procedure fFL1btnCancelClick(Sender: TObject);
    procedure TfEditor1RenderPanelMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TfEditor1RenderPanelMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure TfEditor1RenderPanelMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FrameFL1btnSaveClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure FrameFL1btnCancelClick(Sender: TObject);
  public
    procedure init;
    property frame: TFrameFL read FrameFL1;
    { Public declarations }
  end;

var
  formFL: TformFL;
  SchodyFrm: TSchodiskoFrm;
implementation

{$R *.dfm}

procedure TformFL.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TformFL.fFL1btnCancelClick(Sender: TObject);
begin
  FrameFL1.btnCancelClick(Sender);
  Close;
end;

procedure TformFL.fFL1btnSaveClick(Sender: TObject);
begin
  FrameFL1.btnSaveClick(Sender);
  if FrameFL1.Success then
    Close;
end;

procedure TformFL.FormCreate(Sender: TObject);
begin
 inherited;
 init;
end;

procedure TformFL.FormDestroy(Sender: TObject);
var I: Integer;
begin
  with TfEditor1 do begin
    RenderContext.Relase;
    RenderContext.Free;
    OrthogonalView.Free;
    for I := 0 to Pedestrians.Count -1 do begin
        TPedestrianEditor(Pedestrians[I]).Free;
    end;
    Pedestrians.Free;

    for I := 0 to Obstacles.Count -1 do begin
        TObstacle(Obstacles[I]).Free;
    end;
    Obstacles.Free;

    for I := 0 to LogObstacles.Count - 1 do begin
        TObstacle(LogObstacles[I]).Free;
    end;
    LogObstacles.Free;

    for I := 0 to Spawns.Count - 1 do begin
      TObstacle(Spawns[I]).Free;
    end;
    Spawns.Free;

    for I := 0 to Sinks.Count - 1 do begin
      TObstacle(Sinks[I]).Free;
    end;
    Sinks.Free;

    for I := 0 to PathPoints.Count - 1 do begin
      TObstacle(PathPoints[I]).Free;
    end;
    PathPoints.Free;

  end;
end;


procedure TformFL.FrameFL1btnCancelClick(Sender: TObject);
begin
  FrameFL1.btnCancelClick(Sender);
  Close;
end;

procedure TformFL.FrameFL1btnSaveClick(Sender: TObject);
begin
  FrameFL1.btnSaveClick(Sender);
  if FrameFL1.Success then
    Close;
end;

procedure TformFL.init;
var Pedestrian: TPedestrianEditor;
    I: Integer;
    pos : TVector;
begin
with TfEditor1 do begin
    Application.OnIdle :=  IdleLoop;
    ViewWidth := LevelSize;
    RenderContext := TRenderContext.Create;
    PhysLayRBtn.Checked:= true;
    CreateBtn.Enabled:= true;

    PolyBtn.Enabled:= true;
    CircleBtn.Enabled:= false;
    SpawnBtn.Enabled:= false;
    SinkBtn.Enabled:= false;
    PathPointBtn.Enabled:= false;
    PathWayBtn.Enabled:= false;


    if not RenderContext.Init(RenderPanel.Handle, true) then Caption := 'RenderContext.Init Error';
//    OrthogonalView := TOrthogonalView.Create;
    pos := TVector.Create(LevelSize / 2, LevelSize / 2, 0);
    OrthogonalView := TOrthogonalView.Create(pos, ViewWidth, ViewWidth, 0);

    Pedestrians := TPedestriansList.Create;
    Obstacles := TObstaclesList.Create;
    LogObstacles := TObstaclesList.Create;
    Spawns:= TObstaclesList.Create;
    Sinks:= TObstaclesList.Create;
    PathPoints:= TObstaclesList.Create;
    AllObstacles:= TObstaclesList.Create;
    Path:= TPath2.Create;

    PickedPedestrianIndex := -1;
    PickedObstacleIndex := -1;
    PickedSpawnIndex := -1;
    PickedSinkIndex := -1;
    PickedPathPointIndex := -1;
    PickedPointIndex := -1;
    PickedPointIndex1 := -1;
    PickedPointIndex2 := -1;
    PickedPointIndex3 := -1;
    PolygonObstacle := nil;
    CircleObstacle := nil;
    R := 0.5; G := 0.5; B := 0.5;

    // nastavenie velkosti mriezky
    GridX := 1; GridY := 1;

    Start := false;
    // vytvorenie GL sceny
    InitGLScene;
    Randomize;
    schodyFrm:= TSchodiskoFrm.Create(self);
  end;
end;

procedure TformFL.TfEditor1RenderPanelMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  TfEditor1.RenderPanelMouseDown(Sender, Button, Shift, X, Y);
end;

procedure TformFL.TfEditor1RenderPanelMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  TfEditor1.RenderPanelMouseMove(Sender, Shift, X, Y);
end;

procedure TformFL.TfEditor1RenderPanelMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  TfEditor1.RenderPanelMouseUp(Sender, Button, Shift, X, Y);
end;

end.
